package net.mysoft.budgetsoft.system.action;

import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.YongHu;
import net.mysoft.budgetsoft.system.service.YongHuService;
import net.mysoft.framework.authority.AccessType;
import net.mysoft.framework.authority.Permission;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.model.SessionInfo;
import net.mysoft.framework.util.ExceptionUtil;
import net.mysoft.framework.util.IpUtil;
import net.mysoft.framework.util.ResourceUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author dagongPGP v1.0
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a>
 * 
 * @version $Revision: 1.00 $Date: 2013-02-06 11:02:08
 */
@Action(value = "yongHuAction", results = { 
		@Result(name = "yongHu", location = "/platform/system/yongHu.jsp"), 
		@Result(name = "yongHuAdd", location = "/platform/system/yongHuAdd.jsp"), 
		@Result(name = "yongHuEdit", location = "/platform/system/yongHuEdit.jsp"), 
		@Result(name = "yongHuView", location = "/platform/system/yongHuView.jsp"), 
		@Result(name = "yongHuSelect", location = "/platform/system/yongHuSelect.jsp"), 
		@Result(name = "yongHuSelect2", location = "/platform/system/yongHuSelect2.jsp"), 
		@Result(name = "yongHuSelectView", location = "/platform/system/yongHuSelectView.jsp"), 
		@Result(name = "yongHuInfoPage", location = "/platform/system/yongHuInformation.jsp"), 
		@Result(name = "download", type = "stream", params = { "contentType", "application/octet-stream", "inputName", "inputStream", "contentDisposition", "attachment;filename=${fileName}", "bufferSize", "4096" }) 
})
public class YongHuAction extends BaseAction implements ModelDriven<YongHu> {

	private static final Logger logger = Logger.getLogger(YongHuAction.class);
	@Resource
	private YongHuService yongHuService;
	private String dir;
	private YongHu yongHu = new YongHu();

	public YongHuService getYongHuService() {
		return yongHuService;
	}

	public void setYongHuService(YongHuService yongHuService) {
		this.yongHuService = yongHuService;
	}

	public YongHu getYongHu() {
		return yongHu;
	}

	public void setYongHu(YongHu yongHu) {
		this.yongHu = yongHu;
	}

	public YongHu getModel() {
		return yongHu;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	/**
	 * 用户登录
	 */
	@Permission(access = AccessType.GUEST)
	public void dengLu() {
		Json j = new Json();
		try {
			String dengLuM = yongHu.getDengLuM();
			String miMa = yongHu.getMiMa();
			YongHu u = yongHuService.login(yongHu);
			if (u != null && u.getDengLuM().equals(dengLuM) && u.getMiMa().equals(miMa)) {
				SessionInfo sessionInfo = new SessionInfo();
				sessionInfo.setUserId(u.getBianHao());
				sessionInfo.setLoginName(yongHu.getDengLuM());
				sessionInfo.setPassword(yongHu.getMiMa());
				sessionInfo.setAuthIds(yongHu.getAuthIds());
				sessionInfo.setRoleIds(yongHu.getJueSeBH());
				sessionInfo.setUserName(yongHu.getXingMing());
				sessionInfo.setIp(IpUtil.getIpAddr(ServletActionContext.getRequest()));

				this.getSession().setAttribute(ResourceUtil.getSessionInfoName(), sessionInfo);

				j.setObj(sessionInfo);
				j.setMsg("登录成功！");
				logger.info(j.getMsg());
				j.setSuccess(true);
			} else {
				List list = yongHuService.find(yongHu);
				YongHu uu = null;
				if (list.size() > 0) {
					uu = (YongHu) list.get(0);
				}
				if (uu != null && uu.getZhuangTai() == 12) {
					j.setMsg("您登陆账号已失效！请联系系统管理员");
				} else {
					j.setMsg("登录失败！用户名或密码错误！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.writeJson(j);
	}

	/**
	 * 用户登出
	 */
	@Permission(access = AccessType.GUEST)
	public String logout() {
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}

	/**
	 * YongHu列表页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHu() {
		return "yongHu";
	}

	/**
	 * YongHu查看页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHuView() {
		return "yongHuView";
	}

	/**
	 * YongHu添加页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHuAdd() {
		return "yongHuAdd";
	}

	/**
	 * YongHu编辑页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHuEdit() {
		return "yongHuEdit";
	}

	/**
	 * YongHu选择页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHuSelect() {
		return "yongHuSelect";
	}

	/**
	 * YongHu选择页面
	 * 
	 * @return
	 */
	@Permission(access = AccessType.LOGIN)
	public String yongHuSelect2() {
		return "yongHuSelect2";
	}

	/**
	 * 加载用户个人信息页面
	 * 
	 * @return
	 */
	public String yongHuInfoPage() {
		return "yongHuInfoPage";
	}

	/**
	 * 加载用户信息
	 */
	public void yongHuInfo() {
		Json j = new Json();
		SessionInfo sessionInfo = (SessionInfo) super.getSession().getAttribute(ResourceUtil.getSessionInfoName());
		String bianHao = sessionInfo.getUserId();
		yongHu = yongHuService.get(bianHao);
		yongHu.setMiMa("");
		super.writeJson(yongHu);
	}

	/**
	 * 修改用户个人信息
	 */
	public void editYongHuInfo() {
		Json j = new Json();
		SessionInfo sessionInfo = (SessionInfo) super.getSession().getAttribute(ResourceUtil.getSessionInfoName());
		String miMa = yongHuService.get(sessionInfo.getUserId()).getMiMa();
		logger.debug("旧密码" + miMa);
		logger.debug("新密码" + yongHu.getMiMa());
		if (!miMa.equals(yongHu.getJiuMiM())) {
			j.setMsg("原密码错误！");
		} else {
			try {
				yongHuService.editUserInfo(yongHu);
				j.setSuccess(true);
				j.setMsg("编辑成功！");
			} catch (Exception e) {
				logger.error(ExceptionUtil.getExceptionMessage(e));
				j.setMsg("编辑失败！");
			}
		}
		super.writeJson(j);
	}

	/**
	 * 添加
	 */
	@Permission(access = AccessType.LOGIN)
	public void add() {
		Json j = new Json();
		try {
			yongHuService.add(yongHu);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 修改
	 */
	@Permission(access = AccessType.LOGIN)
	public void edit() {
		Json j = new Json();
		try {
			yongHuService.edit(yongHu);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 删除
	 */
	@Permission(access = AccessType.LOGIN)
	public void delete() {
		Json j = new Json();
		try {
			yongHuService.delete(yongHu);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	/**
	 * 列表
	 */
	@Permission(access = AccessType.LOGIN)
	public void datagrid() {
		try {
			super.writeJson(yongHuService.datagrid(yongHu));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}

	@Permission(access = AccessType.LOGIN)
	public String yongHuSelectView() {
		return "yongHuSelectView";
	}

	@Permission(access = AccessType.LOGIN)
	public void find() {
		try {
			super.writeJson(yongHuService.find(yongHu));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}

	public void check() {
		try {
			super.writeJson(yongHuService.check(yongHu));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			e.printStackTrace();
		}
	}

	public void checkEdit() {
		try {
			//System.out.println("Action + yongHu.getBianHao()=======" + yongHu.getBianHao());
			super.writeJson(yongHuService.checkEdit(yongHu));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			e.printStackTrace();
		}
	}

}
